﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.ApplicationBlocks.Data;

namespace DanhmucThietbi
{
    class NhaCCInfor
    {
        private string _MA_NHACC;

        public string MA_NHACC
        {
            get { return _MA_NHACC; }
            set { _MA_NHACC = value; }
        }

        private string _TEN_NHACC;

        public string TEN_NHACC
        {
            get { return _TEN_NHACC; }
            set { _TEN_NHACC = value; }
        }

        private string _DIA_CHI;

        public string DIA_CHI
        {
            get { return _DIA_CHI; }
            set { _DIA_CHI = value; }
        }

        private string _NGUOI_LHE;

        public string NGUOI_LHE
        {
            get { return _NGUOI_LHE; }
            set { _NGUOI_LHE = value; }
        }

        private string _SO_DT;

        public string SO_DT
        {
            get { return _SO_DT; }
            set { _SO_DT = value; }
        }

        private string _NGUOI_TAO;

        public string NGUOI_TAO
        {
            get
            {
                if (_NGUOI_TAO == null) _NGUOI_TAO = "ADMIN";
                return _NGUOI_TAO;
            }
            set { _NGUOI_TAO = value; }
        }

        private DateTime _NGAY_TAO;

        public DateTime NGAY_TAO
        {
            get { return _NGAY_TAO; }
            set { _NGAY_TAO = value; }
        }

        private string _NGUOI_SUA;

        public string NGUOI_SUA
        {
            get
            {
                if (_NGUOI_SUA == null) _NGUOI_SUA = "ADMIN";
                return _NGUOI_SUA;
            }
            set { _NGUOI_SUA = value; }
        }

        private DateTime _NGAY_SUA;

        public DateTime NGAY_SUA
        {
            get { return _NGAY_SUA; }
            set { _NGAY_SUA = value; }
        }

        private bool _IS_DELETE;

        public bool IS_DELETE
        {
            get { return _IS_DELETE; }
            set { _IS_DELETE = value; }
        }
    }

    class CtrNhaCC
    {
        string myCnn = "";
        public CtrNhaCC()
        {
            myCnn = ConfigurationManager.ConnectionStrings["myConnectionString"].ToString();
        }

        public List<NhaCCInfor> GetAll()
        {
            List<NhaCCInfor> returnList = new List<NhaCCInfor>();
            SqlDataReader ObjRead = SqlHelper.ExecuteReader(myCnn, CommandType.StoredProcedure, "DEV_SP_NHACC_GetAllItem");
            while (ObjRead.Read())
            {
                NhaCCInfor ObjNhacc = new NhaCCInfor();
                ObjNhacc.MA_NHACC = ObjRead["MA_NHACC"].ToString();
                ObjNhacc.TEN_NHACC = ObjRead["TEN_NHACC"].ToString();
                ObjNhacc.DIA_CHI = ObjRead["DIA_CHI"].ToString();
                ObjNhacc.NGUOI_LHE = ObjRead["NGUOI_LHE"].ToString();
                ObjNhacc.SO_DT = ObjRead["SO_DT"].ToString();
                ObjNhacc.NGUOI_TAO = ObjRead["NGUOI_TAO"].ToString();
                returnList.Add(ObjNhacc);
            }
            return returnList;
        }

        public NhaCCInfor GetByID(string MaNHaCC)
        {
            SqlDataReader ObjRead = SqlHelper.ExecuteReader(myCnn, CommandType.StoredProcedure, "DEV_SP_NHACC_GetByID", new SqlParameter("@MA_NHACC", MaNHaCC));
            NhaCCInfor ObjNhacc = null;
            while (ObjRead.Read())
            {
                ObjNhacc = new NhaCCInfor();
                ObjNhacc.MA_NHACC = ObjRead["MA_NHACC"].ToString();
                ObjNhacc.TEN_NHACC = ObjRead["TEN_NHACC"].ToString();
                ObjNhacc.DIA_CHI = ObjRead["DIA_CHI"].ToString();
                ObjNhacc.NGUOI_LHE = ObjRead["NGUOI_LHE"].ToString();
                ObjNhacc.SO_DT = ObjRead["SO_DT"].ToString();
                ObjNhacc.NGUOI_TAO = ObjRead["NGUOI_TAO"].ToString();
                ObjNhacc.NGAY_TAO =(DateTime) ObjRead["NGAY_TAO"];
                ObjNhacc.NGUOI_SUA = ObjRead["NGUOI_SUA"].ToString();
                ObjNhacc.NGAY_SUA = (DateTime)ObjRead["NGAY_SUA"];
            }
            return ObjNhacc;
        }

        public void InsertItem(NhaCCInfor Item)
        {            
            SqlParameter[] Para = new SqlParameter[6];
            Para[0] = new SqlParameter("MA_NHACC", Item.MA_NHACC);
            Para[1] = new SqlParameter("TEN_NHACC", Item.TEN_NHACC);
            Para[2] = new SqlParameter("DIA_CHI", Item.DIA_CHI == null ? (object)DBNull.Value : Item.DIA_CHI);
            Para[3] = new SqlParameter("NGUOI_LHE", Item.NGUOI_LHE == null ? (object)DBNull.Value : Item.NGUOI_LHE);
            Para[4] = new SqlParameter("SO_DT", Item.SO_DT == null ? (object)DBNull.Value : Item.SO_DT);
            Para[5] = new SqlParameter("NGUOI_TAO", Item.NGUOI_TAO);
            try
            {
                SqlHelper.ExecuteNonQuery(myCnn, CommandType.StoredProcedure, "DEV_SP_NHACC_Insert", Para);
            }
            catch (Exception)
            {

                throw new System.ArgumentException(string.Format("Mã nhà cung cấp {0} đã tồn tại, vui lòng chọn mà khác", Item.MA_NHACC));
            }            
        }
        
        public void UpdateItem(NhaCCInfor Item)
        {
            SqlParameter[] Para = new SqlParameter[6];
            Para[0] = new SqlParameter("MA_NHACC", Item.MA_NHACC);
            Para[1] = new SqlParameter("TEN_NHACC", Item.TEN_NHACC);
            Para[2] = new SqlParameter("DIA_CHI", Item.DIA_CHI == null ? (object)DBNull.Value : Item.DIA_CHI);
            Para[3] = new SqlParameter("NGUOI_LHE", Item.NGUOI_LHE == null ? (object)DBNull.Value : Item.NGUOI_LHE);
            Para[4] = new SqlParameter("SO_DT", Item.SO_DT == null ? (object)DBNull.Value : Item.SO_DT);
            Para[5] = new SqlParameter("NGUOI_SUA", Item.NGUOI_SUA);
            try
            {
                SqlHelper.ExecuteNonQuery(myCnn,CommandType.StoredProcedure, "DEV_SP_NHACC_Update", Para);
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        public void DeleteItem(NhaCCInfor Item)
        {
            SqlParameter[] Para = new SqlParameter[2];
            Para[0] = new SqlParameter("MA_NHACC", Item.MA_NHACC);
            Para[1] = new SqlParameter("NGUOI_SUA", Item.NGUOI_SUA);
            SqlHelper.ExecuteNonQuery(myCnn, CommandType.StoredProcedure, "DEV_SP_NHACC_Delete", Para);
        }
    }
}
